//
//  UserInfoEditViewController+Save.m
//  Romens.XSBalance
//
//  Created by 孙博 on 16/3/18.
//  Copyright © 2016年 孙博. All rights reserved.
//

#import "UserInfoEditViewController+Save.h"
#import "userInformation.h"
#import "WebRequestData.h"
#import "ToolHelper.h"
#import "PhPTable.h"
#import "RuntimeStatus.h"
@implementation UserInfoEditViewController(Save)
-(void)SaveData
{
    //网络请求；
    NSMutableDictionary * addMemberDic=[NSMutableDictionary new];
    NSMutableDictionary * jsonArray=[self GetHealthInfo];
    [addMemberDic setObject: jsonArray forKey:@"JSON"];
    [addMemberDic setObject:@"0" forKey:@"USERLASTTIME"];
    [addMemberDic setObject:@"0" forKey:@"LABELLASTTIME"];
           [self Request:[SystemUrl stringByAppendingString:@"/Health"] queryType:@"SynchronizeFamilyUserInfo" postData:addMemberDic success:^(id data){
            
            WebRequestData *data1=(WebRequestData*)data;
            if(!isEmptyString(data1.Error))
            {
                [self SHowMessage:data1.Error type:showError];
                return ;
            }
            PhPTable*table=[[PhPTable alloc] init];
            [ToolHelper jsonParse:data1.RequestData PhPtable:table];
            [self SaveALLUser:table];
            [self SaveResultForExec];

            
        } showString:@"正在同步家庭成员"];
}






-(void)SaveALLUser:(PhPTable*)table
{
    NSMutableArray*Users=[[NSMutableArray alloc] init];
    for (NSDictionary*dic in table.rows) {
        if([[dic objectForKey:@"STATE"] isEqualToString:@"0"])
        {
            continue;
        }
        userInformation*user=[[userInformation alloc] init];
        [Users addObject:user];
        user.GUID=[dic objectForKey:@"USERGUID"];
        user.USERID=[dic objectForKey:@"ID"];
        user.USERNAME=[dic objectForKey:@"USERNAME"];
        user.RELATIONSHIP=[dic objectForKey:@"RELATIONSHIP"];
        user.ISONESELF=[dic objectForKey:@"ISONESELF"];
        user.UPDATED=[dic objectForKey:@"UPDATED"];
        user.CREATED=[dic objectForKey:@"CREATED"];
        NSMutableArray*array= [dic objectForKey:@"JSON"];
         user.DetailData=[array copy];
        for (NSDictionary*ardic in array) {
            NSString*key=[ardic objectForKey:@"KEY"];
            NSString*Value=[ardic objectForKey:@"VALUE"];
            if([key isEqualToString:@"HEIGHT"])
            {
              user.Height=Value;
            }
            
            if([key isEqualToString:@"BIRTHDAY"])
            {
                user.Birthday=Value;
            }
            if([key isEqualToString:@"SEX"])
            {
                user.SEX=Value;
            }
            if([key isEqualToString:@"TARGHTWEIGHT"])
            {
                user.TarghtWeight=Value;
            }
            if([key isEqualToString:@"WEIGHT_UNIT"])
            {
                user.WEIGHT_UNIT=Value;
            }
            if([key isEqualToString:@"HEIGHT_UNIT"])
            {
                user.HEIGHT_UNIT=Value;
           }
            if([key isEqualToString:@"LifeState"])
            {
                user.LifeState=Value;
            }
           
      }
    }
    [[RuntimeStatus instance] SaveUsers:Users];
    
}


/**
 *  设置KEYVALUE
 *
 *  @return <#return value description#>
 */
-(NSMutableDictionary *)GetHealthInfo
{
    NSMutableDictionary*User=[[NSMutableDictionary alloc] init];
    
    NSString*userGuid=[ToolHelper NewGuid];
    if(!isEmptyString(self.userinfor.USERID))
    {
        userGuid=self.userinfor.USERID;
    }
    [User setObject:userGuid forKey:@"ID"];
    [User setObject:self.userinfor.USERNAME forKey:@"USERNAME"];
    [User setObject:self.userinfor.RELATIONSHIP  forKey:@"RELATIONSHIP"];
    [User setObject:self.userinfor.ISONESELF forKey:@"ISONESELF"];
    
    
    NSMutableArray *array=[[NSMutableArray alloc] init];
    NSMutableDictionary*dic=[[NSMutableDictionary alloc] init];
    [dic setObject:userGuid forKey:@"USERID"];
    [dic setObject:@"SEX" forKey:@"KEY"];
    [dic setObject:self.userinfor.SEX forKey:@"VALUE"];
    [dic setObject:[self GetKeyGUID:[dic objectForKey:@"KEY"]] forKey:@"GUID"];
    [array addObject:dic];
    
    dic=[[NSMutableDictionary alloc] init];
    [dic setObject:userGuid forKey:@"USERID"];
    [dic setObject:@"BIRTHDAY" forKey:@"KEY"];
    [dic setObject:self.userinfor.Birthday forKey:@"VALUE"];
    [dic setObject:[self GetKeyGUID:[dic objectForKey:@"KEY"]] forKey:@"GUID"];
    [array addObject:dic];
    
    
    dic=[[NSMutableDictionary alloc] init];
    [dic setObject:userGuid forKey:@"USERID"];
    [dic setObject:@"HEIGHT" forKey:@"KEY"];
    [dic setObject:self.userinfor.Height forKey:@"VALUE"];
     [dic setObject:[self GetKeyGUID:[dic objectForKey:@"KEY"]] forKey:@"GUID"];
    [array addObject:dic];
    
    dic=[[NSMutableDictionary alloc] init];
    [dic setObject:userGuid forKey:@"USERID"];
    [dic setObject:@"TARGHTWEIGHT" forKey:@"KEY"];
    [dic setObject:self.userinfor.TarghtWeight forKey:@"VALUE"];
     [dic setObject:[self GetKeyGUID:[dic objectForKey:@"KEY"]] forKey:@"GUID"];
    [array addObject:dic];
    
    dic=[[NSMutableDictionary alloc] init];
    [dic setObject:userGuid forKey:@"USERID"];
    [dic setObject:@"LifeState" forKey:@"KEY"];
    [dic setObject:self.userinfor.LifeState forKey:@"VALUE"];
     [dic setObject:[self GetKeyGUID:[dic objectForKey:@"KEY"]] forKey:@"GUID"];
    [array addObject:dic];
    
    dic=[[NSMutableDictionary alloc] init];
    [dic setObject:userGuid forKey:@"USERID"];
    [dic setObject:@"HEIGHT_UNIT" forKey:@"KEY"];
    [dic setObject:self.userinfor.HEIGHT_UNIT forKey:@"VALUE"];
     [dic setObject:[self GetKeyGUID:[dic objectForKey:@"KEY"]] forKey:@"GUID"];
    [array addObject:dic];
    
    
    dic=[[NSMutableDictionary alloc] init];
    [dic setObject:userGuid forKey:@"USERID"];
    [dic setObject:@"WEIGHT_UNIT" forKey:@"KEY"];
    [dic setObject:self.userinfor.WEIGHT_UNIT forKey:@"VALUE"];
     [dic setObject:[self GetKeyGUID:[dic objectForKey:@"KEY"]] forKey:@"GUID"];
    [array addObject:dic];
    
    
    [User setObject:array forKey:@"JSON"];
    return User;
}

-(NSString*)GetKeyGUID:(NSString*)Key
{
    NSString*Guid=[ToolHelper NewGuid] ;
    if(self.userinfor.DetailData)
    {
        for (NSDictionary*dic in self.userinfor.DetailData) {
            if([[dic objectForKey:@"KEY"] isEqualToString:Key])
            {
                return [dic objectForKey:@"GUID"];
            }
        }
    }
    return Guid;
}



-(void)DeleteUserInfo
{
    //网络请求；
    NSMutableDictionary * addMemberDic=[NSMutableDictionary new];
    [addMemberDic setObject:self.userinfor.USERID forKey:@"ID"];
    [self Request:[SystemUrl stringByAppendingString:@"/Health"] queryType:@"DeleUserInfo" postData:addMemberDic success:^(id data){
        
        WebRequestData *data1=(WebRequestData*)data;
        if(!isEmptyString(data1.Error))
        {
            [self SHowMessage:data1.Error type:showError];
            return ;
        }
       
        NSDictionary*dic= [ToolHelper JsonToDictionary:data1.RequestData];
      if([dic objectForKey:@"USER"]==0)
      {
          [self SHowMessage:@"删除用户失败" type: showError];
          return;
      }
        
        if([dic objectForKey:@"USERINFO"] ==0)
        {
            [self SHowMessage:@"删除用户信息失败" type: showError];
            return;
        }
        [self DeleteSelectUser];
        
        
    } showString:@"正在删除家庭成员"];
}

-(void)DeleteSelectUser
{
    NSMutableArray*array= [RuntimeStatus instance].ALLUsers;
    userInformation*deleteuser=nil;
    for (userInformation*user in array ) {
      if([user.USERID isEqualToString:self.userinfor.USERID])
      {
          deleteuser=user;
          break;
      }
          
    }
    [array removeObject:deleteuser];
    [[RuntimeStatus instance] SaveUsers:array];
     [self SaveResultForExec];
}
@end
